﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace UtilsExtensionsAndHelpers.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Shortcut to Double.TryParse(str, out num);
        /// </summary>
        public static bool IsNumber(this string str)
        {
            double num;
            return Double.TryParse(str, out num);
        }

        /// <summary>
        /// Shortcut to !str.Contains(content);
        /// </summary>
        public static bool DoesntContain(this string str, string content)
        {
            return !str.Contains(content);
        }

        /// <summary>
        /// Shortcut to !str.StartsWith(content);
        /// </summary>
        public static bool DoesntStartWith(this string str, string content)
        {
            return !str.StartsWith(content);
        }

        /// <summary>
        /// Shortcut to !str.EndsWith(content);
        /// </summary>
        public static bool DoesntEndWith(this string str, string content)
        {
            return !str.EndsWith(content);
        }

        /// <summary>
        /// Shortcut str.Split(new[] {splitStr}, StringSplitOptions.RemoveEmptyEntries);
        /// </summary>
        public static IEnumerable<string> SplitWith(this string str, string splitStr)
        {
            return str.Split(new[] { splitStr }, StringSplitOptions.RemoveEmptyEntries);
        }

        /// <summary>
        /// Shortcut str.Split(new[] { splitStr }, options);
        /// </summary>
        public static IEnumerable<string> SplitWith(this string str, string splitStr, StringSplitOptions options)
        {
            return str.Split(new[] { splitStr }, options);
        }

        /// <summary>
        /// Shortcut str.Split(new[] { "," }, StringSplitOptions.None);
        /// </summary>
        public static IEnumerable<string> CsvSplit(this string str)
        {
            return str.Split(new[] { "," }, StringSplitOptions.None);
        }


        /// <summary>
        /// Calls .ToLower().Trim() on both, compares and returns the result.
        /// </summary>
        public static bool EqlLowerTrimed(this string str, string against)
        {
            if (against == null) return false;
            return str.ToLower().Trim().Equals(against.ToLower().Trim());
        }

        /// <summary>
        /// Shortcut to String.Format()
        /// </summary>
        public static String FormatWith(this String str, object arg0)
        {
            return String.Format(str, arg0);
        }
        /// <summary>
        /// Shortcut to String.Format()
        /// </summary>
        public static String FormatWith(this String str, object arg0, object arg1)
        {
            return String.Format(str, arg0, arg1);
        }
        /// <summary>
        /// Shortcut to String.Format()
        /// </summary>
        public static String FormatWith(this String str, object arg0, object arg1, object arg2)
        {
            return String.Format(str, arg0, arg1, arg2);
        }
        /// <summary>
        /// Shortcut to String.Format()
        /// </summary>
        public static String FormatWith(this String str, params object[] args)
        {
            return String.Format(str, args);
        }
        /// <summary>
        /// Shortcut to String.Format()
        /// </summary>
        public static String FormatWith(this String str, IFormatProvider provider, params object[] args)
        {
            return String.Format(provider, str, args);
        }

        /// <summary>
        /// Shortcut to int x = 0; Int32.TryParse(str, out x); return x;
        /// </summary>
        public static Int32 ToInt(this string str)
        {
            int x = 0;
            Int32.TryParse(str, out x);
            return x;
        }

        /// <summary>
        /// Shortcut to int x = 0; Double.TryParse(str, out x); return x;
        /// </summary>
        public static Double ToDouble(this string str)
        {
            Double x = 0;
            Double.TryParse(str, out x);
            return x;
        }

        /// <summary>
        /// Shortcut to rgx.Replace(new Regex("[^a-zA-Z0-9 -]"), "");
        /// </summary>
        public static string RemoveAllNonAlphanumeric(this string str)
        {
            var rgx = new Regex("[^a-zA-Z0-9 -]");
            return rgx.Replace(str, "");
        }

        /// <summary>
        /// Shortcut to str.Replace(strToRemove, String.Empty);
        /// </summary>
        public static string RemoveStr(this string str, string strToRemove)
        {
            return str.Replace(strToRemove, String.Empty);
        }

        /// <summary>
        /// Shortcut to  strsToRemove.Aggregate(str, (current, strToRemove) => current.Replace(strToRemove, String.Empty));
        /// </summary>
        public static string RemoveStr(this string str, string[] strsToRemove)
        {
            return strsToRemove.Aggregate(str, (current, strToRemove) => current.Replace(strToRemove, String.Empty));
        }
    }
}
